package com.software.practicecenter.controller;

import com.software.practicecenter.domain.Result;
import com.software.practicecenter.entity.Question;
import com.software.practicecenter.service.QuestionService;
import com.software.practicecenter.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.domain.Page;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @Author xiaoliao
 * @Date 2023/5/31 15:05
 */
@RestController
@RequestMapping("/question")
@RefreshScope
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @GetMapping("/list_page")
    public Page<Question> findAll(
            @RequestParam(name = "pageNo", required = false, defaultValue = "1") String pageNo,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10") String pageSize
    ){
        Page<Question> page = questionService.findAll(Integer.valueOf(pageNo)-1, Integer.valueOf(pageSize));
        return page;
    }

    @GetMapping("/findQuestionByBelongChapter/{belongChapter}")
    public List<Question> findQuestionByBelongChapter(@PathVariable String belongChapter) {
        return questionService.findQuestionByBelongChapter(belongChapter);
    }

    @RequestMapping("/findByIdIn")
    public List<Question> findByIdIn(@RequestBody Collection<String> ids) {
        return questionService.findByIdIn(ids);
    }

    @GetMapping("/findQuestionById/{id}")
    public Question findQuestionById(@PathVariable String id){
        return questionService.findQuestionById(id);
    }

    @GetMapping("/findQuestionByBelongChapterAndQuestion")
    public Page<Question> findQuestionByBelongChapterAndQuestion(
            @RequestParam(name = "belongChapter", required = false) String belongChapter,
            @RequestParam(name = "question", required = false) String question,
            @RequestParam(name = "pageNo", required = false, defaultValue = "1") String pageNo,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10") String pageSize){
        Page<Question> page = questionService.findQuestionByBelongChapterAndQuestion(belongChapter, question, Integer.valueOf(pageNo)-1, Integer.valueOf(pageSize));
        return page;
    }

    @PostMapping("/addQuestion")
    public Result<Question> addQuestion(
            @RequestBody Question question){
        Question save = questionService.addQuestion(question);
        return ResultUtil.success(save);
    }

    @RequestMapping(value = "/updateQuestion")
    public Result<Question> updateQuestion(@RequestBody Question question){
        questionService.updateQuestion(question);
        return ResultUtil.success(question);
    }

    @GetMapping(value = "/deleteQuestionById/{id}")
    public Result<Question> deleteQuestionById(@PathVariable String id){
        questionService.deleteQuestionById(id);
        return ResultUtil.success(null);
    }

}
